package com.lingshu.app.ui.report;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.lingshu.app.R;
import com.lingshu.app.base.BaseActivity;
import com.lingshu.app.config.Constants;
import com.lingshu.app.databinding.ActivityReportBinding;
import com.lingshu.app.model.enums.ScreeningReportType;
import com.lingshu.app.model.enums.ScreeningType;
import com.lingshu.app.model.register.RegisterResponse;
import com.lingshu.app.model.register.RegisterScreenshotResponse;
import com.lingshu.core.eventbus.EventBusManager;
import com.lingshu.core.eventbus.EventData;
import com.lingshu.core.eventbus.EventKey;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * @author 于定金
 * @version 1.0
 * @Date 2024/1/10 16:33
 * @Description
 */
@AndroidEntryPoint
public class ReportActivity extends BaseActivity<ActivityReportBinding, ReportViewModel> {
    private ReportActivityAdapter adapter;
    private List<Fragment> fragmentList;
    private RegisterResponse currentRegister;
    private RegisterScreenshotResponse screenshotResponse1;
    private RegisterScreenshotResponse screenshotResponse2;

    /**
     * 获取当前页面的布局资源ID
     *
     * @return 布局资源ID
     */
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_report;
    }

    /**
     * 初始化ViewModel
     */
    @Override
    protected void initViewModel() {
        viewModel = new ViewModelProvider(this).get(ReportViewModel.class);
    }

    /**
     * 绑定ViewModel
     */
    @Override
    protected void bindViewModel() {
        dataBinding.setViewModel(viewModel);
    }

    @Override
    protected void handleIntent(Intent intent) {
        super.handleIntent(intent);
        Bundle bundle = intent.getBundleExtra(Constants.REGISTER);
        currentRegister = (RegisterResponse) bundle.getSerializable(Constants.REGISTER_INFO);
        screenshotResponse1 = (RegisterScreenshotResponse) bundle.getSerializable(Constants.SCREENSHOT1);
        screenshotResponse2 = (RegisterScreenshotResponse) bundle.getSerializable(Constants.SCREENSHOT2);
    }

    @Override
    protected void init() {
        super.init();
        viewModel.initData();
        dataBinding.btnBack.setOnClickListener(view -> {
            finish();
        });
        viewModel.selectedOption.observe(this, checkedId -> {
            if (checkedId == R.id.radio_report_screenshot) {
                changeFragment(0);
            } else if (checkedId == R.id.radio_report_form) {
                changeFragment(1);
            } else if (checkedId == R.id.radio_report_signature) {
                changeFragment(2);
            }
        });
        getFragmentList();

        adapter = new ReportActivityAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList);
        dataBinding.viewpagerReportContent.setAdapter(adapter);
        dataBinding.viewpagerReportContent.setPageTransformer(new ViewPager2.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                page.setAlpha(1 - Math.abs(position));
            }
        });
        dataBinding.viewpagerReportContent.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeFragment(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBusManager.post(new EventData<>(EventKey.Refresh, "refresh"));
    }

    private void getFragmentList() {
        fragmentList = new ArrayList<>();
        FormScreenshotFragment screenshotFragment = new FormScreenshotFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.SCREENSHOT1, screenshotResponse1);
        bundle.putSerializable(Constants.SCREENSHOT2, screenshotResponse2);
        bundle.putSerializable(Constants.REGISTER_INFO, currentRegister);
        screenshotFragment.setArguments(bundle);
        fragmentList.add(screenshotFragment);
        if (currentRegister.getScreeningType() == ScreeningType.THYROID) {
            if (currentRegister.getScreeningReportType() == ScreeningReportType.SIMPLE) {
                FormThyroidFragment thyroidFragment = new FormThyroidFragment();
                thyroidFragment.setArguments(bundle);
                fragmentList.add(thyroidFragment);
            } else if (currentRegister.getScreeningReportType() == ScreeningReportType.STRUCTURE) {
                FormThyroidStructureFragment formThyroidStructureFragment = new FormThyroidStructureFragment();
                formThyroidStructureFragment.setArguments(bundle);
                fragmentList.add(formThyroidStructureFragment);
            }
        } else if (currentRegister.getScreeningType() == ScreeningType.CAROTID) {
            if (currentRegister.getScreeningReportType() == ScreeningReportType.SIMPLE) {
                FormCarotidFragment carotidFragment = new FormCarotidFragment();
                carotidFragment.setArguments(bundle);
                fragmentList.add(carotidFragment);
            } else if (currentRegister.getScreeningReportType() == ScreeningReportType.STRUCTURE) {
                FormCarotidStructureFragment formCarotidStructureFragment = new FormCarotidStructureFragment();
                formCarotidStructureFragment.setArguments(bundle);
                fragmentList.add(formCarotidStructureFragment);
            }
        } else if (currentRegister.getScreeningType() == ScreeningType.BREAST) {
            if (currentRegister.getScreeningReportType() == ScreeningReportType.SIMPLE) {
                FormBreastFragment breastFragment = new FormBreastFragment();
                breastFragment.setArguments(bundle);
                fragmentList.add(breastFragment);
            } else if (currentRegister.getScreeningReportType() == ScreeningReportType.STRUCTURE) {
                FormBreastStructureFragment formBreastStructureFragment = new FormBreastStructureFragment();
                formBreastStructureFragment.setArguments(bundle);
                fragmentList.add(formBreastStructureFragment);
            }
        }
        fragmentList.add(new FormSignatureFragment());
    }

    private void changeFragment(int position) {
        switch (position) {
            case 0:
                dataBinding.viewpagerReportContent.setCurrentItem(0);
                viewModel.selectedOption.postValue(R.id.radio_report_screenshot);
                break;
            case 1:
                dataBinding.viewpagerReportContent.setCurrentItem(1);
                viewModel.selectedOption.postValue(R.id.radio_report_form);
                break;
            case 2:
                dataBinding.viewpagerReportContent.setCurrentItem(2);
                viewModel.selectedOption.postValue(R.id.radio_report_signature);
                break;
        }
    }

    public List<RegisterScreenshotResponse> getScreenshotList() {
        List<RegisterScreenshotResponse> list = new ArrayList<>();
        if (screenshotResponse1 != null && screenshotResponse2 != null) {
            list.add(screenshotResponse1);
            list.add(screenshotResponse2);
            return list;
        }
        return null;
    }
}
